--
-- Author: ping.wang
-- Date: 2019-03-06 18:47:51
--
-- PvpDuelPartnerRender
--

local ClassRef = app.mvc.createListViewCell(...)
local NewTeamMemberHeadRender = require("app.team.views.renders.NewTeamMemberHeadRender")
--data =
function ClassRef.createWithData(data, node)
    local sprite = ClassRef.extend(node or cc.CSLoader:createNode(Res.PvpDuelPartnerNode))
    if node == nil then
        ccui.Helper:doLayout(sprite)
    end
    sprite:init(data)
    return sprite
end
function ClassRef.create(node)
    return ClassRef.createWithData(nil, node)
end

--
function ClassRef:init(data)
    self.super.init(self, data)

    for i = 1, 5 do
        self._ui.Panel:findChild("Button_"..i):setVisible(false)
    end

    self._ui.Panel_partner:setVisible(false)
    self._talentIcon = self._ui.Panel_bg:findChild("icon2") --天赋ICON
    self._headIcon = NewTeamMemberHeadRender.create(self._ui.head)

    self._selectedBtn = self._ui.Panel_partner:findChild("selected_btn") --是否上阵的btn
    self._selectedBtn:addClickEventListener(function() -- 切换随从 雇佣 状态
        if self._data  then
            local partner = self._data.partner           
            if self._data.type == "eliteMatch" then
                -- 如果是精英决斗赛
                local myDuelInfo = UD.csEliteMatch:getEliteMatchDuelInfo()
                if not self._data.isOn and myDuelInfo:isMaxPartnerOnFight(4) then
                    display.pushToast(L("duelGametips36"))
                    return
                end
            else
                --要判定当前是否已经上阵了2个partner 若已经上阵2个 则飘字提示
                local model = app:getInst("PvpModel")
                local myDuelInfo = model:getDuelInfo().myInfo
                if not self._data.isOn and myDuelInfo:isMaxPartnerOnFight() then
                    display.pushToast(L("duelGametips36"))
                    return
                end
            end

            self:setpartnerBtnStatus( not self._data.isOn )
            self:dispatchCellEvent("changeDuelPartnerStatus",{partnerId = partner:getId(), isOnBattle = self._data.isOn})
        end
    end)

    self:setContentSize( self._ui.Panel:getContentSize() )
    self:refresh()
end

function ClassRef:refresh()
    if self._data then
        self:setVisible( true )
        local data = self._data
        local partner = data.partner
        self._ui.name:setString(partner:getName())
        self._ui.combat_num:setString(tostring(partner:getCombatPower()))
        self._headIcon:setVisible(true)
        self._headIcon:setIcon(partner:getIcon())
        self._headIcon:setClassIcon(partner:getJobIcon())
        self._headIcon:setLv( partner:getLevel() )
        self._headIcon:setQualityIcon( partner:getQuality() )
        self._ui.title:setString( partner:getJobName() )
        self._talentIcon:loadTexture(partner:getJobIcon())

        self._ui.Panel_partner:setVisible(true)
        self:setpartnerBtnStatus(data.isOn)
    else
        self:setVisible( false )
    end
end

function ClassRef:setpartnerBtnStatus(isOn)
    self._selectedBtn:setBright( not isOn )

    if self._data then
        self._data.isOn = isOn
    end
end

function ClassRef:onBtnClick(cb)
    self._onClick = cb
end

return ClassRef
